<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('分配规则') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('分配类型')" prop="allocationType">
        <el-select v-model="queryParams.allocationType" :placeholder="$t('分配类型')" clearable>
          <el-option
            v-for="dict in allocationTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索')
        }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fund:allocationRules:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="fundAllocationRulesList" border>
      <el-table-column
        fixed="left"
        :label="$t('序号')"
        type="index"
        align="center"
        show-overflow-tooltip
        width="50"
      >
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('出资人类型')"
        align="center"
        prop="investorTypeLabel"
        min-width="100"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.investorTypeLabel }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('合伙人类型')"
        align="center"
        prop="partnerType"
        :formatter="partnerTypeOptionsFormat"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        :label="$t('分配规则')"
        align="center"
        prop="allocationRules"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        :label="$t('分配类型')"
        align="center"
        prop="allocationType"
        :formatter="allocationTypeOptionsFormat"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        :label="$t('有无Hurdle')"
        align="center"
        prop="hurdle"
        :formatter="hurdleTypeOptionsFormat"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        :label="$t('Hurdle计算方式')"
        align="center"
        prop="hurdleCalMethod"
        :formatter="hurdleCalMethodOptionsFormat"
        min-width="140"
        show-overflow-tooltip
      />
      <el-table-column :label="$t('Hurdle比例(%)')" align="center" prop="hurdleRatio" min-width="120">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.hurdleRatio) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('每年计息天数')" align="center" prop="yearInterestDay" min-width="120">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.yearInterestDay) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('Carry比例LP(%)')"
        align="center"
        prop="carryRatioLp"
        min-width="120"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.partnerType != 'GP'">{{ moneyFormat(scope.row.carryRatioLp) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        :label="$t('操作')"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['fund:allocationRules:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}
          </el-button>
          <el-button
            v-hasPermi="['fund:allocationRules:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分配规则对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <fundAllocationRulesForm ref="fundAllocationRulesForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  delFundAllocationRules,
  exportFundAllocationRules,
  listFundAllocationRules
} from '@/api/fund/allocationRules/fundAllocationRules'
import fundAllocationRulesForm from './fundAllocationRulesForm.vue'

export default {
  components: {
    fundAllocationRulesForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 分配规则表格数据
      fundAllocationRulesList: [],
      // 合伙人类型
      partnerTypeOptions: [],
      // 分配类型
      allocationTypeOptions: [],
      // 有无hurdle
      hurdleTypeOptions: [],
      // hurdle计算方式
      hurdleCalMethodOptions: [],
      // 弹出层标题
      title: '分配规则',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        fundId: this.$route.query.id,
        allocationType: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getDicts('partner_type').then(response => {
      this.partnerTypeOptions = response.data
    })
    this.getDicts('allocation_type').then(response => {
      this.allocationTypeOptions = response.data
    })
    this.getDicts('hurdle_type').then(response => {
      this.hurdleTypeOptions = response.data
    })
    this.getDicts('hurdle_cal_method').then(response => {
      this.hurdleCalMethodOptions = response.data
    })
    this.getList()
  },
  methods: {
    /** 查询分配规则列表 */
    getList() {
      this.loading = true
      listFundAllocationRules(this.queryParams).then(response => {
        this.fundAllocationRulesList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 合伙人类型
    partnerTypeOptionsFormat(row) {
      return this.selectDictLabel(this.partnerTypeOptions, row.partnerType)
    },
    // 分配类型
    allocationTypeOptionsFormat(row) {
      return this.selectDictLabel(this.allocationTypeOptions, row.allocationType)
    },
    // 有无hurdle
    hurdleTypeOptionsFormat(row) {
      return this.selectDictLabel(this.hurdleTypeOptions, row.hurdle)
    },
    // 有无hurdle
    hurdleCalMethodOptionsFormat(row) {
      return this.selectDictLabel(this.hurdleCalMethodOptions, row.hurdleCalMethod)
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: undefined,
        allocationRules: undefined,
        allocationType: undefined,
        hurdle: undefined,
        hurdleRatio: undefined,
        yearInterestDay: undefined,
        carryRatioLp: undefined,
        carryRatioGp: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.fundAllocationRulesForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.fundAllocationRulesForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delFundAllocationRules(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return exportFundAllocationRules(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>
